//
//  GTTopNavGroupView.m
//  GTSliderDemo
//
//  Created by yate1996 on 15/7/21.
//  Copyright (c) 2015年 yate1996. All rights reserved.
//
#define BUTTONITEMWIDTH   70
#define SCREENW [UIScreen mainScreen].bounds.size.width

#import "GTTopNavGroupView.h"

@implementation GTTopNavGroupView

- (id)initWithFrame:(CGRect)frame ButtonItems:(NSArray *)aItemsArray
{
    self = [super initWithFrame:frame];
    if (self) {
        if (ButtonArray == nil) {
            ButtonArray = [[NSMutableArray alloc] init];
        }
        if (ScrollView == nil) {
            ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
            ScrollView.showsHorizontalScrollIndicator = NO;
            ScrollView.bounces = NO;
        }
        if (ItemInfoArray == nil) {
            ItemInfoArray = [[NSMutableArray alloc]init];
        }
        [ItemInfoArray removeAllObjects];
        [self createMenuItems:aItemsArray];
    }
    return self;
}
-(void)createMenuItems:(NSArray *)itemsArray
{
    int i = 0;
    float menuWidth = 0.0;
    for (NSDictionary *lDic in itemsArray) {
        NSString *TitleStr = [lDic objectForKey:TITLEKEY];
        float ButtonWidth = [[lDic objectForKey:TITLEWIDTH] floatValue];
        UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
        [Button setBackgroundColor:[UIColor orangeColor]] ;
        [Button setTitle:TitleStr forState:UIControlStateNormal];
        [Button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
        [Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [Button setTag:i];
        [Button addTarget:self action:@selector(menuButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [Button setFrame:CGRectMake(menuWidth, 0, ButtonWidth, self.frame.size.height)];
        [ScrollView addSubview:Button];
        [ButtonArray addObject:Button];
        
        menuWidth += ButtonWidth;
        i++;
        
        //保存button资源信息，同时增加button.oringin.x的位置，方便点击button时，移动位置。
        NSMutableDictionary *vNewDic = [lDic mutableCopy];
        [vNewDic setObject:[NSNumber numberWithFloat:menuWidth] forKey:TOTALWIDTH];
        [ItemInfoArray addObject:vNewDic];
    }
    
    [ScrollView setContentSize:CGSizeMake(menuWidth, self.frame.size.height)];
    [self addSubview:ScrollView];
    // 保存menu总长度，如果小于320则不需要移动，方便点击button时移动位置的判断
    TotalWidth = menuWidth;
}
#pragma mark - 其他辅助功能
#pragma mark 取消所有button点击状态
-(void)changeButtonsToNormalState{
    for (UIButton *Button in ButtonArray) {
        Button.selected = NO;
    }
}

#pragma mark 模拟选中第几个button
-(void)clickButtonAtIndex:(NSInteger)aIndex{
    UIButton *vButton = [ButtonArray objectAtIndex:aIndex];
    [self menuButtonClicked:vButton];
}

#pragma mark 改变第几个button为选中状态，不发送delegate
-(void)changeButtonStateAtIndex:(NSInteger)Index{
    UIButton *Button = [ButtonArray objectAtIndex:Index];
    [self changeButtonsToNormalState];
    Button.selected = YES;
    [self moveScrolViewWithIndex:Index];
}

#pragma mark 移动button到可视的区域
-(void)moveScrolViewWithIndex:(NSInteger)Index{
    if (ItemInfoArray.count < Index) {
        return;
    }
    if (TotalWidth <= SCREENW) {
        return;
    }
    NSDictionary *buttonDictionary = [ItemInfoArray objectAtIndex:Index];
    float buttonPiont = [[buttonDictionary objectForKey:TOTALWIDTH] floatValue];
    if (buttonPiont >= 300) {
        if ((buttonPiont + 180) >= ScrollView.contentSize.width) {
            [ScrollView setContentOffset:CGPointMake(ScrollView.contentSize.width - SCREENW, ScrollView.contentOffset.y) animated:YES];
            return;
        }
        
        float vMoveToContentOffset = buttonPiont - 180;
        if (vMoveToContentOffset > 0) {
            [ScrollView setContentOffset:CGPointMake(vMoveToContentOffset, ScrollView.contentOffset.y) animated:YES];
        }
    }else{
        [ScrollView setContentOffset:CGPointMake(0, ScrollView.contentOffset.y) animated:YES];
        return;
    }
}

#pragma mark - 点击事件
-(void)menuButtonClicked:(UIButton *)aButton{
    [self changeButtonStateAtIndex:aButton.tag];
    if ([_delegate respondsToSelector:@selector(didMenuHrizontalClickedButtonAtIndex:)]) {
        [_delegate didMenuHrizontalClickedButtonAtIndex:aButton.tag];
    }
}



@end
